<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="巡更路线" prop="watchNo">
      <el-input v-model="dataForm.watchNo" placeholder="巡更路线"></el-input>
    </el-form-item>
    <el-form-item label="所属组别" prop="watchGroup">
      <el-input v-model="dataForm.watchGroup" placeholder="所属组别"></el-input>
    </el-form-item>
    <el-form-item label="巡更人员" prop="watchMan">
      <el-input v-model="dataForm.watchMan" placeholder="巡更人员"></el-input>
    </el-form-item>
    <el-form-item label="现场联系电话" prop="linkPhone">
      <el-input v-model="dataForm.linkPhone" placeholder="现场联系电话"></el-input>
    </el-form-item>
      <el-form-item label="巡更时间范围" prop ="watchTimes">
        <el-date-picker
          v-model="dataForm.watchTimes"
          type="datetimerange"
          value-format = "yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
<!--    <el-form-item label="巡更开始时间" prop="watchStartTime">-->
<!--      <el-input v-model="dataForm.watchStartTime" placeholder="巡更开始时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="巡更结束时间" prop="watchEndTime">-->
<!--      <el-input v-model="dataForm.watchEndTime" placeholder="巡更结束时间"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/watchpeople'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          watchNo: '',

          watchGroup: '',

          watchMan: '',

          linkPhone: '',

          watchStartTime: '',

          watchEndTime: '',

          createTime: '',

          updateTime: '',

          delFlag: '',
          watchTimes:[]

        },
        dataRule: {
          watchNo: [
            { required: true, message: '巡更路线不能为空', trigger: 'blur' }
          ],

          watchGroup: [
            { required: true, message: '所属组别不能为空', trigger: 'blur' }
          ],

          watchMan: [
            { required: true, message: '巡更人员不能为空', trigger: 'blur' }
          ],

          linkPhone: [
            { required: true, message: '现场联系电话不能为空', trigger: 'blur' }
          ],

          watchTimes: [
            { type:'array',required: true, message: '请选择巡更时间范围', trigger: 'change' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
              let tmpArr = []
              tmpArr.push(this.dataForm.watchStartTime)
              tmpArr.push(this.dataForm.watchEndTime)
              this.dataForm.watchTimes = tmpArr
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              this.dataForm.watchStartTime = this.dataForm.watchTimes[0];
              this.dataForm.watchEndTime = this.dataForm.watchTimes[1];
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
              this.dataForm.watchStartTime = this.dataForm.watchTimes[0];
              this.dataForm.watchEndTime = this.dataForm.watchTimes[1];
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
